package com.lx.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lx.bean.Admin;
import com.lx.mapper.AdminMapper;
import com.lx.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class AdminServiceImpl implements AdminService {
    @Autowired
    private AdminMapper adminMapper;

    @Override
    public int addAdmin(Admin admin) {
        int insert = adminMapper.insert(admin);
        return insert;
    }

    @Override
    public Admin login(String username, String password) {
        QueryWrapper<Admin> wrapper = new QueryWrapper<>();
        wrapper.eq("username",username)
                .eq("password",password);
        Admin admin = adminMapper.selectOne(wrapper);
        return admin;
    }

    @Override
    public Map showPageAdmin(Integer page, Integer limit) {
        //创建分页对象 构造分页参数
        Page<Admin> pageObj = new Page<>(page,limit);
        //调用分页查询的方法 selectPage
        Page<Admin> adminPage = adminMapper.selectPage(pageObj, null);
        //获取数据总条数
        long total = adminPage.getTotal();
        //得到查询的真实数据
        Map map = new HashMap<>();
        List<Admin> admins = adminPage.getRecords();
        map.put("count",total);
        map.put("admins",admins);
        return map;
    }

}
